/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.client.ui;


import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import redora.client.util.GWTViewUtil;

import java.util.Date;

/**
 * Anchor style widget that allows lazy retrieval of typically a n-to-1 relationship.
 * When the related pojo is not in the cache, it will show a 'see details' anchor.
 * When clicked by the user, it will be fetched from the server.<br>
 * The widget uses the cache service and has a listeners on it's pojo. When
 * the pojo is updated elsewhere, the change will synced. This also implies that as
 * soon as somewhere the pojo is fetched, this widget will be updated.
 * @author Nanjing Redorange (www.red-orange.cn)
 */
public class TimeCell extends AbstractCell<Date> {

    interface Template extends SafeHtmlTemplates {
        @Template("<div title=\"{0}\">{1}</div>")
        SafeHtml time(String fullTime, String shortTime);
    }
    Template template;

    /**
     * Construct a new {@link TimeCell}
     */
    public TimeCell() {
        super("click", "keydown");
        if (template == null) {
            template = GWT.create(Template.class);
        }
    }

    @Override
    public void render(Cell.Context context, Date value, SafeHtmlBuilder sb) {
        sb.append(template.time(
                GWTViewUtil.formatFullTime(value), GWTViewUtil.formatShortTime(value)));
    }
}

